package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.model.dao.PurchaseMapper;
import com.sservice.model.dao.PurchaseOrderMapper;
import com.sservice.model.entity.Goods;
import com.sservice.model.entity.Purchase;
import com.sservice.model.entity.PurchaseOrder;
import com.sservice.model.vo.PurchaseOrderVo;
import com.sservice.service.service.IAdvanceReceiptService;
import com.sservice.service.service.IGoodsService;
import com.sservice.service.service.PurchaseOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PurchaseOrderMapper dataDao;
    @Autowired
    private PurchaseMapper purchaseDao;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IAdvanceReceiptService arService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dataDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PurchaseOrder record) {
        return dataDao.insert(record);
    }

    @Override
    public int insertSelective(PurchaseOrder record) {
        return dataDao.insertSelective(record);
    }

    @Transactional
    @Override
    public synchronized Object createOrder(PurchaseOrder order) {
//        record.get
        Purchase purchase = purchaseDao.selectByPrimaryKey(Integer.valueOf(order.getPurchaseId()));
        if (purchase.getNumber().compareTo(order.getNumber()) == -1) {
            return "采购数量小于下单数量，不能下单";
        }
        Goods goods = goodsService.selectByPrimaryKey(Integer.valueOf(purchase.getGoodsId()));
        order.setTaxRatio(BigDecimal.valueOf(goods.getTaxRatio()));
        order.setPack(purchase.getPackSpec());
        order.setGoodsName(goods.getName());
        order.setQuality(purchase.getQualitySpec());
        order.setCurrentPrice(order.getCurrentPrice());
        order.setOriginalPrice(purchase.getPrice());
        order.setStoreName(purchase.getDeliverPlace()); // 供货地
        purchase.setNumber(purchase.getNumber().subtract(order.getNumber()));
        purchase.setNumberCharging(purchase.getNumberCharging().add(order.getNumber()));
        purchaseDao.updateByPrimaryKeySelective(purchase);
        order.setTitle(purchase.getTitle());
        order.setDeliverPlace(purchase.getDeliverPlace());
//        order.setOriginalPrice(purchase.getPrice());
        order.setNo(Constants.generateServiceNO("C", Constants.generateLimitNumberStr(dataDao.queryCurrentMonthCount() + 1, 5)));
        order.setTotal(order.getNumber().multiply(order.getCurrentPrice()));
        int po = dataDao.insertSelective(order);
//        if (po > 0) {
//            AdvanceReceipt ar = new AdvanceReceipt();
//            ar.setType("3"); // 采购应付
//            ar.setTotalDeposit(order.getTotal());
//            ar.setOrderId(String.valueOf(order.getId()));
//            ar.setClienteleId(order.getClienteleId());
//            ar.setUnitCode(order.getUnitCode());
//            ar.setNo(order.getNo());
//            arService.insertSelective(ar);
//        }
        return po;
    }

    @Override
    public PurchaseOrder selectByPrimaryKey(Integer id) {
        return dataDao.selectByPrimaryKey(id);
    }

    @Transactional
    @Override
    public synchronized Object forceEnd(PurchaseOrder po) {
        PurchaseOrder purchaseOrder = dataDao.selectByPrimaryKey(po.getId());
        if (Double.compare(purchaseOrder.getNumberReceiving().doubleValue(), 0.0) == 1) {
            return "有单据在执行中，请处理完毕再进行此操作!";
        }
        BigDecimal bg = purchaseOrder.getActualReceiveTotal().subtract((purchaseOrder.getCurrentPrice().multiply(purchaseOrder.getNumberActualReceive())));
        if (bg.compareTo(BigDecimal.valueOf(0.0)) == -1) {
            return "实际付款金额必须大于实际收货货款才能强制执行完毕!";
        }

        po.setApplyBack("0");
        po.setStatus("3"); // 强制执行完毕
        Purchase purchase = new Purchase();
        Purchase purchase1 = purchaseDao.selectByPrimaryKey(Integer.valueOf(purchaseOrder.getPurchaseId()));
        BigDecimal d = purchaseOrder.getNumber().subtract(purchaseOrder.getNumberActualReceive());
        purchase.setNumber(purchase1.getNumber().add(d));
        purchase.setNumberCharging(purchase1.getNumberCharging().subtract(d));
        purchase.setId(Integer.valueOf(purchaseOrder.getPurchaseId()));
        purchaseDao.updateByPrimaryKeySelective(purchase);
//        po.setNumber(purchaseOrder.getNumberActualReceive());
        return dataDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public int updateByPrimaryKeySelective(PurchaseOrder record) {


        return dataDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public Object updateWidthPrice(PurchaseOrder record) {
        if (!record.getCheckStatus().equals("0")) {
            return "采购订单已经审核完毕, 不能修改";
        }
        return dataDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PurchaseOrder record) {
        return dataDao.updateByPrimaryKey(record);
    }

    @Override
    public int queryCount(PurchaseOrderVo record) {
        return dataDao.queryByCount(record);
    }

    @Override
    public int queryCurrentMonthCount() {
        return dataDao.queryCurrentMonthCount();
    }

    @Override
    public List<PurchaseOrderVo> queryByPage(PurchaseOrderVo record) {
        return dataDao.queryByPage(record);
    }

    @Override
    public int countByPurchaseId(PurchaseOrder record) {
        return dataDao.countByPurchaseId(record);
    }

    @Override
    @Transactional
    public int changeNumber(Integer id, BigDecimal receiving, BigDecimal received) {
        PurchaseOrder po = dataDao.selectByPrimaryKey(id);
        po.setNumberActualReceive(po.getNumberActualReceive().add(received));
        po.setNumberReceiving(po.getNumberReceiving().add(receiving));
        if(po.getNumber().compareTo(po.getNumberActualReceive())==0){
            po.setStatus(null);
        }
        return dataDao.updateByPrimaryKeySelective(po);
    }

    @Override
    @Transactional
    public int changeMoney(Integer id, BigDecimal applying, BigDecimal actual) {
        PurchaseOrder po = dataDao.selectByPrimaryKey(id);
        po.setApplyPayTotal(po.getApplyPayTotal().add(applying));
        po.setActualReceiveTotal(po.getActualReceiveTotal().add(actual));
        if (po.getApplyPayTotal().compareTo(BigDecimal.ZERO) == 0) {
            //'订单状态:0未执行，1执行中，2终止执行，3强制执行完毕，4执行完毕
            po.setApplyPay("4");
        } else {
            po.setApplyPay("1");
        }
        if (po.getActualReceiveTotal().compareTo(BigDecimal.ZERO) == 0) po.setPayStatus("0");
        if (po.getActualReceiveTotal().compareTo(BigDecimal.ZERO) == 1 && po.getActualReceiveTotal().compareTo(po.getTotal()) == -1)
            po.setPayStatus("1");
        if (po.getActualReceiveTotal().compareTo(po.getTotal()) == 0) po.setPayStatus("2"); // 完全付款
        return dataDao.updateByPrimaryKeySelective(po);
    }
}